﻿#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QMutex>
#include <QDateTime>
#include <QDebug>

#if defined(Q_OS_WIN)
#include <Windows.h>
#endif

class Logger
{
public:
    static Logger& instance();

    // 初始化日志系统
    bool init(const QString &filePath, bool alsoConsoleOutput = false);

    // 手动写日志
    void log(QtMsgType type, const QString &msg);

private:
    explicit Logger();
    ~Logger();

    Logger(const Logger&) = delete;
    Logger& operator=(const Logger&) = delete;

    // Qt 消息处理器
    static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);

    // C++ 异常/崩溃捕获
    static void setupCrashHandler();

#if defined(Q_OS_WIN)
    static LONG WINAPI windowsExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo);
#else
    static void unixSignalHandler(int sig);
#endif

    QFile m_logFile;
    QTextStream m_stream;
    bool m_consoleOutput = true;
    QMutex m_mutex;
};

#endif // LOGGER_H
